package Runde8;

public class Company 
{
	
	private OneWayWorker[] workers;
	private Storage[] storage;
	private ThreadGroup workergroup;
	private boolean inOperation;
	
	/**
	 * 
	 * @precondition kc >= 1; tc >= 5; sc >= 2; a1,a2 >= 1
	 * @postcondition Creates a new company with given capacities, workingtimes and hunting trips
	 */
	public Company (int kc, int tc, int sc, int ws1t, int ws2t, int kt, int tc1t, int tc2t, int tc3t, int tc4t, int tc5t, int lg1t, int lg2t, int a1, int a2) {
		
		storage = new Storage[4];
		workers = new OneWayWorker[10];
		workergroup = new ThreadGroup("workergroup");
		
		storage[0] = new CoolingHouse(kc, 2);
		storage[1] = new Table(tc , 1);
		storage[2] = new StonePit(sc , 5);
		storage[3] = new VillageSquare((a1+a2)*5, 2);
		workers[0] = new Hunter(storage[0],ws1t,1,this,a1);
		workers[1] = new Hunter(storage[0],ws2t,1,this,a2);
		workers[2] = new Cook(storage[0],storage[1],kt,1,5,this);
		workers[3] = new Woodcutter(storage[1],storage[2],tc1t,1,1,this);
		workers[4] = new Woodcutter(storage[1],storage[2],tc2t,1,1,this);
		workers[5] = new Woodcutter(storage[1],storage[2],tc3t,1,1,this);
		workers[6] = new Woodcutter(storage[1],storage[2],tc4t,1,1,this);
		workers[7] = new Woodcutter(storage[1],storage[2],tc5t,1,1,this);
		workers[8] = new Logistician(storage[2],storage[3],lg1t,2,2,this);
		workers[9] = new Logistician(storage[2],storage[3],lg2t,2,2,this);

	}
	
	/**
	 * @postcondition Starts the company's operation
	 */
	public void start() {
		inOperation = true;
		for (int i = 0; i < workers.length; i++) {
			new Thread(workergroup,workers[i]).start();
		}
	}
	
	/**
	 * @postcondition Stops the company's operation
	 */
	public void stop() {
		inOperation = false;
		workergroup.interrupt();
		report();
	}
	
	/**
	 * @postcondition Prints out the company's current status
	 */
	public void report() {
		for(int i = 0; i<workers.length; i++)
			System.out.println(workers[i].report());
		for(int i = 0; i<storage.length; i++)
			System.out.println(storage[i].report());
	}
	
	/**
	 * @postcondition Returns true or false, whether the company is in operation or not
	 */
	public boolean inOperation() { return inOperation; }
		
}
